<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addFormVisible = true">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="标题" value="1"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok"/>
      </div>
    </div>

    <!-- table -->
    <el-table stripe style="width: 100%" :data="tableData" :key="key" border fit highlight-current-row
              ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="140" align="center" prop="msg_id" label="序号">
      </el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="120" align="center" prop="msg_title" label="标题">
       </el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="110" align="center" prop="msg_content" label="内容">
      </el-table-column>
      <el-table-column v-if="colData[3].istrue" prop="create_time" label="创建时间" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.create_time| activateTime}}</template>
      </el-table-column>
      <el-table-column v-if="colData[4].istrue" prop="start_time" label="通知时间" min-width="140" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.start_time}}</span>
          <br>
          <span>{{scope.row.end_time}}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[5].istrue" prop="batch" label="批次" min-width="140" align="center">
        <template slot-scope="scope">
          <el-select v-show="scope.row.batchArr.length>0" v-model="scope.row.batchArr" placeholder="请选择" multiple disabled>
            <el-option
              v-for="item in scope.row.batchNameArr"
              :key="item.id"
              :label="item.batch"
              :value="item.id.toString()">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[6].istrue" min-width="110" align="center" prop="admin_name" label="创建人">
      </el-table-column>
      <el-table-column label="操作" min-width="200" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="openEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="primary" >推送</el-button>
            <el-button size="mini" @click="openDelMessage(scope.row.msg_id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]"
                     layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>

    <!-- 添加弹窗 -->
    <el-dialog title="消息通知" :visible.sync="addFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="50%" @close="addFormclose">
      <el-form :model="addForm" :rules="rules" ref="ruleForm" class="diolog_adapt">
        <el-form-item label="通知标题:" label-width="120px" prop="msg_title">
          <el-input v-model="addForm.msg_title" placeholder="请输入通知标题"></el-input>
        </el-form-item>
        <el-form-item label="通知内容:" label-width="120px" prop="msg_content">
          <el-input v-model="addForm.msg_content" placeholder="请输入通知内容"></el-input>
        </el-form-item>
        <el-form-item label="通知时间:" label-width="120px" prop="msg_time">
          <div class="block">
          <el-date-picker
            v-model="addForm.msg_time"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            value-format="timestamp"
            :default-time = "['00:00:00','23:59:59']"
            end-placeholder="结束日期">
          </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="通知批次" label-width="120px" prop="batch">
          <el-select v-model="addForm.batch" :multiple-limit="10" placeholder="通知批次" style="flex:1" multiple>
            <el-option v-for="item in lotoptions" :key="item.id" :label="item.batch"
                       :value="item.id.toString()"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通知频次:" label-width="120px" prop="interval">
         <span>每隔<el-input v-model="addForm.interval" style="width: 50px" size="mini"></el-input>分钟显示一次</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" size="mini" round @click="editSelfMessage('ruleForm')">确 定
        </el-button>
        <el-button v-else type="primary" @click="getAddMessage('ruleForm')" size="mini" round>确定</el-button>
      </div>
    </el-dialog>

    <!-- 删除弹窗start -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents"
                   :deleteText="deleteDialogs.deleteText"/>
    <!-- 删除弹窗end -->
  </div>
</template>

<script>
  import UploadExcelComponent from "@/components/UploadExcelSn/index.vue";
  import dynamicTable from "@/components/DynamicTable";
  import {getBatchTwo} from "@/api/equipment.js";
  import deleteDialog from "@/components/deleteDialog";
  import tableMini from '@/utils/tableMini.js';
  import { addMessageControl, queryMessageData,editSelfMessageData,deleteSelfMessage } from '@/api/launcherMade'

  export default {
    mixins: [tableMini],
    components: {UploadExcelComponent, dynamicTable, deleteDialog},
    data() {
      return {
        checkboxVal: ["序号", "标题", "内容", "创建时间", "通知时间", "批次", "创建人"],
        tableData: [],
        select: "", //搜索条件
        //编辑按钮控制
        editSubjects: false,
        subjectId: 0,
        listQuery: {
          startSize: 1,
          pageSize: 10,
          userId: localStorage.getItem("ID")
        },
        addFormVisible: false,
        lotoptions: [],
        addForm: {
          msg_title: '',
          msg_content: '',
          msg_time: [],
          interval: '',
          batch: '',
        },
        rules: {
          msg_title: [{required: true, message: "请输入通知标题", trigger: "blur"}],
          msg_content: [{required: true, message: "请输入通知内容", trigger: "blur"}],
          msg_time: [{type: 'array', required: true, message: '请选择通知时间', trigger: 'change'}],
          // batch: [{required: true, message: "请选择批次", trigger: "blur"}]
        },
      }
    },
    created() {
      this.getMessageTableData()
      this.getBatchTwos()
    },
    methods: {
      openDelMessage(msgId){
        this.deleteDialogs.show = true
        this.deleteDialogs.deleteId = msgId
      },
      //删除按钮事件
      deleteEvents() {
        let params = {
          userId: localStorage.getItem("ID"),
          last_ip: sessionStorage.getItem("Ip"),
          msg_id: this.deleteDialogs.deleteId
        }
        deleteSelfMessage(params).then(res => {
          this.$message.success(res.msg)
          this.getMessageTableData()
        })
      },
      //添加
      getAddMessage(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addForm = JSON.parse(JSON.stringify(this.addForm))
            addForm.last_ip = sessionStorage.getItem("Ip")
            addForm.userId = localStorage.getItem("ID")
            addMessageControl(addForm).then(res => {
              this.$message.success(res.msg)
              this.getMessageTableData()
              this.addFormVisible = false
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      openEdit(row) {
        this.addFormVisible=true
        this.editSubjects=true
        this.addForm=row
        this.addForm.batch=row.batchArr
      },
      //编辑信息
      editSelfMessage(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let editForm = JSON.parse(JSON.stringify(this.addForm))
            editForm.last_ip = sessionStorage.getItem("Ip")
            editForm.userId = localStorage.getItem("ID")
            editSelfMessageData(editForm).then(res => {
              this.$message.success(res.msg)
              this.getMessageTableData()
              this.addFormVisible = false
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      //根据ID获取批次列表
      getBatchTwos() {
        let data = {userId: localStorage.getItem("ID")}
        getBatchTwo(data).then(res => {
          this.lotoptions = res.data
        }).catch(err => {
          this.lotoptions = []
        })
      },
      getMessageTableData() {
        queryMessageData(this.listQuery).then(res => {
          if(res.code==200){
            this.tableData = res.data
            this.total = res.total
          }else{
            this.$message.error(this.res.msg)
          }
        }).catch(err => {
          this.tableData = []
          this.total = 0
        })
      },
      //编辑添加表单关闭事件
      addFormclose() {
        this.editSubjects=false
        this.$nextTick(() => {
          this.$refs.ruleForm.resetFields();
          this.addForm = this.$options.data().addForm
        })
      },
      Refresh(){
        this.listQuery = { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), name: undefined}
        this.getMessageTableData()
      },
      //搜索,1:通知标题
      idnameSearch() {
        if (!this.select) return this.$message.warning("请先选择查询条件");
        this.listQuery.startSize = 1
        this.listQuery.pageSize = 10
        switch (this.select) {
          case '1':
            this.listQuery.msg_title = this.searchTable
            this.getMessageTableData();
            break;
        }
      },
    }
  }
</script>
